<?php 
	class Manage_branch extends CI_Controller{
		public function __construct(){
			parent::__construct();
			$this->load->library('session');
			$this->load->library('form_validation');
			$this->load->library('cismarty');
			$this->load->model("apanel/template");
			$this->load->model("apanel/db_branch");
			$this->load->model("apanel/db_news");
			$this->load->library('pagination');
		}
		
		public function branch_list($st = ""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			list($item,$num,$page,$total) = $this->db_branch->listAll();
			$this->cismarty->assign("Num_Rows",$num);
			$this->cismarty->assign("item",$item);
			$this->cismarty->assign("pagelink",$page);
			$this->cismarty->assign("total",$total);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
			//== template
			$mark  = 4;
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(2,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_branch/branch_delAll"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/branch/branch_list.html');
		}
		
		public function branch_add(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("status","status","required");
				$this->form_validation->set_rules("customer_pkid","Customer","required");
				$this->form_validation->set_rules("name_en","name","required");
				$this->form_validation->set_rules("name_zh_CN","name","trim");
				$this->form_validation->set_rules("name_th"," name","trim");
				$this->form_validation->set_rules("description_en","Description","trim|required");
				$this->form_validation->set_rules("description_zh_CN","Description","trim");
				$this->form_validation->set_rules("description_th","Description","trim");
				$this->form_validation->set_rules("address","Address","required");
				$this->form_validation->set_rules("tel","Telephone","trim|numeric");
			
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("customer_pkid",set_value("customer_pkid"));
					$this->cismarty->assign("name_en",set_value('name_en'));
					$this->cismarty->assign("name_zh_CN",set_value('name_zh_CN'));
					$this->cismarty->assign("name_th",set_value('name_th'));
					$this->cismarty->assign("description_en",set_value('description_en'));
					$this->cismarty->assign("description_zh_CN",set_value('description_zh_CN'));
					$this->cismarty->assign("description_th",set_value('description_th'));
					$this->cismarty->assign("address",set_value('address'));
					$this->cismarty->assign("tel",set_value('tel'));
			
					//set error
					$this->cismarty->assign("errorcustomer",form_error('customer'));
					$this->cismarty->assign("errorname_en",form_error('name_en'));
					$this->cismarty->assign("errordescription_en",form_error('description_en'));
					$this->cismarty->assign("erroraddress",form_error('address'));
					$this->cismarty->assign("errorstatus",form_error('status'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/branch/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 310;
							$config['height'] = 210;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"customer_pkid"=>$this->input->post("customer"),
								"name_en"=>trim($this->input->post('title_en')),
								"name_zh_CN"=>trim($this->input->post('title_cn')),
								"name_th"=>trim($this->input->post('title_th')),
								"description_en"=>addslashes(trim($this->input->post('desc_en'))),
								"description_zh_CN"=>addslashes(trim($this->input->post('desc_cn'))),
								"description_th"=>addslashes(trim($this->input->post('desc_th'))),
								"description_th"=>addslashes(trim($this->input->post('desc_th'))),
								"address"=>addslashes(trim($this->input->post('address'))),
								"tel"=>addslashes(trim($this->input->post('tel'))),
								"img"=>$picname,
//								"img2"=>$picname[1],
//								"img3"=>$picname[2],
//								"img4"=>$picname[3],
								"status"=>$this->input->post('status'),
								"created_by"=>$this->session->userdata("sess_username"),
								"created_date"=>date("Y-m-d H:i:s")
						);
						$res = $this->db->insert('mobile_branch',$data);
						if($res) redirect('manage_branch/branch_list/success');
						else  redirect('manage_branch/branch_list/fail');
					}
				}
			}//end post
				
				
			$this->cismarty->assign("tiny",$this->template->tinymce());
			$this->cismarty->assign("customer",$this->db_news->customer_list());
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(2,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart('manage_branch/branch_add'));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/branch/branch_add.html');
		}
		
		
	}
?>